<?php

namespace App\Http\Controllers;


use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Request;
use ServiceClass\Article;

class ArticleController extends Controller
{
    //
    public function __construct(Article $article){

        $this->article = $article;
    }

    public function addArticle(){


        $input = Request::only('article_content','article_shortContent','article_title','article_categories');

        $input['article_content'] = urlencode($input['article_content']);
        $result = $this->article->addArticle($input);
        if($result == 1){

            return json_encode(['msg'=>"添加文章成功"]);
        }else{

            return json_encode(['msg'=>"服务器错误"]);
        }
    }

    public function deleteArticle(){

        $article_id   = Request::only('article_id');

        $result = $this->article->deleteArticle($article_id);
        if($result == 1){

            return json_encode(['msg'=>"删除成功"]);
        }else{

            return json_encode(['msg'=>"服务器错误"]);
        }
    }

    public function searchArticle(){

        $article_id   = Request::only('article_id');

        if(Redis::get($article_id['article_id'])){

                return Redis::get($article_id['article_id']);
        }else{

            $result = $this->article->searchArticle($article_id);
            if($result == 0){

                return json_encode(['msg'=>"服务器错误"]);
            }else {

                $result[0]->article_content = urldecode($result[0]->article_content);
                $data = json_encode($result);
                Redis::set($article_id['article_id'] ,$data);
                return $data;

            }
        }
    }

    public function selectArticleList(){
        $data = Request::all();
        $page = $data['page'];

        $where['article_categories'] = $data['article_categories'];
        if(empty($where['article_categories'])){
            $result = $this->article->selectArticleList('article','3','',$page);
        }else{
            $result = $this->article->selectArticleList('article','3',$where,$page);

        }
        $totalPage = $result[1];
        for($i = 0;$i<count($result[0]);$i++){
            foreach ($result[0][$i] as $key => $value){
                $returnData[$i][$key] = $value;
            }
        }
        for($j=0;$j<count($returnData);$j++){
            $resultData[$j]['article_id'] = $returnData[$j]['article_id'];
            $resultData[$j]['article_CreateTime'] = $returnData[$j]['article_CreateTime'];
            $resultData[$j]['article_shortContent'] = $returnData[$j]['article_shortContent'];
            $resultData[$j]['article_categories'] = $returnData[$j]['article_categories'];
            $resultData[$j]['article_UpdateTime'] = $returnData[$j]['article_UpdateTime'];
            $resultData[$j]['article_title'] = $returnData[$j]['article_title'];
            $resultData[$j]['opreate'] = $returnData[$j]['opreate'];
        }
        $return = array($resultData,$totalPage);

        return json_encode($return);
    }


    public function updateArticle(){


        $input = Request::only('article_content','article_shortContent','article_title','article_categories','article_id');


        $result = $this->article->updateArticle($input);

        if($result == 1){

            $data = json_encode($input);
            Redis::set($input['article_id'] ,$data);

            return json_encode(['msg'=>"修改文章成功"]);
        }else{

            return json_encode(['msg'=>"修改文章失败"]);
        }
    }

}
